/**
* ...
* @author Default
* @version 0.1
*/

package bdz.cms.core {
	
	import bdz.cms.core.CMSCore;
	import bdz.cms.core.CMSConfig;
	import bdz.cms.core.events.CMSEvent;
	import bdz.cms.vo.*;
	import bdz.common.managers.UploadManager;
	import bdz.common.events.UploadManagerEvent;
	import flash.utils.Dictionary;
	
	import flash.net.URLVariables;
	public class CMSWriter extends CMSCore {
		
		private var uploader:UploadManager;
		
		function CMSWriter(config:CMSConfig){
			uploader = new UploadManager();
			super(config);
		}
		public function addCategory(cat:CategoryVO):void{
			var vars:URLVariables = new URLVariables();
			vars.method = "category.insert";
			vars.cat_name = cat.name;
			vars.parent_id = cat.parent_id || 0;
			vars.order = cat.order;
			vars.cat_desc = cat.description;
			processData(CMSEvent.CATEGORY_ADDED, vars);
		}
		public function updateCategory(cat:CategoryVO):void{
			var vars:URLVariables = new URLVariables();
			vars.method = "category.edit";
			vars.id = cat.id;
			vars.cat_name = cat.name;
			vars.order = cat.order;
			vars.parent_id = cat.parent_id;
			vars.cat_desc = cat.description;
			processData(CMSEvent.CATEGORY_MODIFIED, vars);
		}
		public function deleteCategory(cat:CategoryVO):void{
			var vars:URLVariables = new URLVariables();
			vars.method = "category.delete";
			vars.id = cat.id;
			processData(CMSEvent.CATEGORY_DELETED, vars);
		}
		public function addContent(cont:ContentVO):void{
			var vars:URLVariables = new URLVariables();
			vars.method = "content.insert";
			vars.title = cont.title;
			vars.parent_id = cont.category;
			vars.description = cont.description;
			vars.order = cont.order;
			if(cont.meta){
				vars.metakeys = "";
				vars.metavalues = "";
				
				for(var prop in cont.meta){
					vars.metakeys += prop + "|";
					vars.metavalues += cont.meta[prop].value + "|";
				}
				vars.metavalues = String(vars.metavalues).substring(0, vars.metavalues.length-1);
				vars.metakeys = String(vars.metakeys).substring(0, vars.metakeys.length-1);
			}
			processData(CMSEvent.CONTENT_ADDED, vars);
		}
		public function updateContent(cont:ContentVO):void{
			var vars:URLVariables = new URLVariables();
			vars.method = "content.edit";
			vars.id = cont.id;
			vars.title = cont.title;
			vars.parent_id = cont.category;
			vars.order = cont.order;
			vars.description = cont.description;
			if(cont.meta){
				vars.metakeys = "";
				vars.metavalues = "";
				vars.metaids = "";
				for(var prop in cont.meta){
					vars.metakeys += prop + "|";
					vars.metavalues += cont.meta[prop].value + "|";
					if(cont.meta[prop].id){
						vars.metaids += cont.meta[prop].id + "|";
					}
				}
				
				vars.metavalues = String(vars.metavalues).substring(0, vars.metavalues.length-1);
				vars.metakeys = String(vars.metakeys).substring(0, vars.metakeys.length-1);
				vars.metaids = String(vars.metaids).substring(0, vars.metaids.length-1);
			}
			
			processData(CMSEvent.CONTENT_MODIFIED, vars)
		}
		public function deleteContent(cont:ContentVO):void{
			var vars:URLVariables = new URLVariables();
			vars.method = "content.delete";
			vars.id = cont.id;
			processData(CMSEvent.CONTENT_DELETED, vars);
		}
		
		public function addMedia(media:MediaVO):void{
			var vars:URLVariables = new URLVariables();
			vars.method = "content.insert";
			vars.type = media.type;
			vars.parent_id = media.category;
			vars.location = media.location;
			vars.order = media.order;
			if(media.meta){
				vars.metakeys = "";
				vars.metavalues = "";
				for(var prop in media.meta){
					vars.metakeys += prop + "|";
					vars.metavalues += media.meta[prop].value + "|";
				}
				vars.metavalues = String(vars.metavalues).substring(0, vars.metavalues.length-1);
				vars.metakeys = String(vars.metakeys).substring(0, vars.metakeys.length-1);
			}
			processData(CMSEvent.CONTENT_ADDED, vars);
		}
		public function updateMedia(media:MediaVO):void{
			var vars:URLVariables = new URLVariables();
			vars.method = "content.edit";
			vars.id = media.id;
			vars.type = media.type;
			vars.parent_id = media.category;
			vars.location = media.location;
			vars.order = media.order
			if(media.meta){
				vars.metakeys = "";
				vars.metavalues = "";
				vars.metaids = "";
				for(var prop in media.meta){
					vars.metakeys += prop + "|";
					vars.metavalues += media.meta[prop].value + "|";
					if(media.meta[prop].id){
						vars.metaids += media.meta[prop].id + "|";
					}
				}
				vars.metavalues = String(vars.metavalues).substring(0, vars.metavalues.length-1);
				vars.metakeys = String(vars.metakeys).substring(0, vars.metakeys.length-1);
				vars.metaids = String(vars.metaids).substring(0, vars.metaids.length-1);
			}
			processData(CMSEvent.MEDIA_MODIFIED, vars)
		}
		public function deleteMedia(media:MediaVO):void{
			var vars:URLVariables = new URLVariables();
			vars.method = "content.delete";
			vars.id = media.id;
			processData(CMSEvent.MEDIA_DELETED, vars);
		}
		
		public function addMeta(key:String, value:*, linkid:Number, usetable:String):void{
			var vars:URLVariables = new URLVariables();
			vars.method = "meta.insert";
			vars.key = key;
			vars.value = value;
			vars.linkid = linkid;
			vars.use_table = usetable;
			
			processData(CMSEvent.META_ADDED, vars);
		}
		public function updateMeta(id:Number, key:String, value:*, linkid:Number, usetable:String):void{
			var vars:URLVariables = new URLVariables();
			vars.method = "meta.edit";
			vars.id = id;
			vars.key = key;
			vars.value = value;
			vars.linkid = linkid;
			vars.use_table = usetable;
			
			
			processData(CMSEvent.META_MODIFIED, vars)
		}
		public function deleteMeta(id:Number):void{
			var vars:URLVariables = new URLVariables();
			vars.method = "meta.delete";
			vars.id = id;
			processData(CMSEvent.META_DELETED, vars);
		}
		public function browseForFile():void{
			
		}
		public function browseForImage():void{
			
		}
		
		
	}
	
}